﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    enum Stimulus { none, left, right }

    public partial class Form1 : Form
    {
        Random randomGen = new Random();

        Stimulus stimulating = Stimulus.none;

        Stimulus reaction = Stimulus.none;

        Timer timer;

        ReactionTimeFeedback reactionTimeFeedback;

        int reactionMs;

        public Form1()
        {
            InitializeComponent();

            reactionMs = 500;
            reactionTimeFeedback = new ReactionTimeFeedback(reactionMs, 2, 0.8);

            timer = new Timer();
            timer.Interval = getInterval();
            timer.Tick += timer1_Tick;
            timer.Start();
        }

        private int getInterval()
        {
            return 500 + randomGen.Next(3500);
        }

        private void handleReaction()
        {
            bool correct = reaction == stimulating;
            if (correct)
            {
                resultLabel.Text = "right";
                resultLabel.ForeColor = Color.Green;
            }
            else
            {
                resultLabel.Text = "wrong";
                resultLabel.ForeColor = Color.Red;
            }

            leftLabel.ForeColor = Color.Black;
            rightLabel.ForeColor = Color.Black;
            stimulating = Stimulus.none;
            reaction = Stimulus.none;

            reactionMs = reactionTimeFeedback.nextReactionMs(correct);

            listBox1.Items.Add(formatReaction());
        }

        private string formatReaction()
        {
            int n = (reactionMs + 50) / 100;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < n; ++i)
            {
                sb.Append('*');
            }

            return sb.ToString();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer.Stop();
            timer.Tick -= timer1_Tick;

            if (stimulating == Stimulus.none)
            {
                bool left = randomGen.Next(2) != 0;
                Label label = left ? leftLabel : rightLabel;
                label.ForeColor = Color.Red;
                stimulating = left ? Stimulus.left : Stimulus.right;
                reaction = Stimulus.none;
                resultLabel.Text = "";

                timer = new Timer();
                timer.Interval = reactionMs;
                timer.Tick += timer1_Tick;
                timer.Start();
            }
            else
            {
                handleReaction();

                timer = new Timer();
                timer.Interval = getInterval();
                timer.Tick += timer1_Tick;
                timer.Start();
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
#if false
            if (stimulating != Stimulus.none)
            {
                if (e.KeyCode == Keys.L)
                {
                    reaction = Stimulus.left;
                }
                else if (e.KeyCode == Keys.R)
                {
                    reaction = Stimulus.right;
                }

                if (reaction != Stimulus.none)
                {
                    handleReaction();
                }
            }
#endif
        }

        private void listBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (stimulating != Stimulus.none)
            {
                if (e.KeyCode == Keys.L)
                {
                    reaction = Stimulus.left;
                }
                else if (e.KeyCode == Keys.R)
                {
                    reaction = Stimulus.right;
                }

                if (reaction != Stimulus.none)
                {
                    handleReaction();
                }
            }
        }
    }
}
